package com.aaa.system.service.impl;

import com.aaa.entity.SysDictData;
import com.aaa.entity.SysDictType;
import com.aaa.system.dao.SysDictDataDao;
import com.aaa.system.dao.SysDictTypeDao;
import com.aaa.system.service.SysDictDataService;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * @author 荆豪峰
 * @version 1.0
 * #Date: 2023/2/28 10:23
 */
@Service
public class SysDictDataServiceImpl extends ServiceImpl<SysDictDataDao, SysDictData> implements SysDictDataService {
     @Autowired
     private SysDictDataDao sysDictDataDao;

    @Override
    public Result list(Integer currentPage, Integer pageSize,SysDictData sysDictData) {
        QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
        wrapper.like(sysDictData.getDictLabel()!=null,"dict_label",sysDictData.getDictLabel());
        PageHelper.startPage(currentPage, pageSize);
        List<SysDictData> sysDictTypes = sysDictDataDao.selectList(wrapper);
        PageInfo<SysDictData> sysDictTypePageInfo = new PageInfo<>(sysDictTypes);
        return new Result(2000,"成功",sysDictTypePageInfo);
    }

    @Override
    public Result delete(Integer dictCode) {
        int i = sysDictDataDao.deleteById(dictCode);
        return new Result(2000,"成功",i);

    }

    @Override
    public Result insert(SysDictData sysDictData) {
         int insert = sysDictDataDao.insert(sysDictData);
        return new Result(2000,"添加成功",insert);
    }

    @Override
    public Result update(SysDictData sysDictData) {
        if (sysDictData.getStatus().equals("正常")) {
            sysDictData.setStatus("0");
        }else if (sysDictData.getStatus().equals("停用")) {
            sysDictData.setStatus("1");
        }
        if (sysDictData.getIsDefault().equals("是")) {
            sysDictData.setIsDefault("1");
        }else if (sysDictData.getIsDefault().equals("否")) {
            sysDictData.setIsDefault("0");
        }
      int i = sysDictDataDao.updateById(sysDictData);
        return new Result(2000,"修改成功",i);
    }

    @Override
    public Result deletes(String[] dictCode) {
        sysDictDataDao.deleteBatchIds(Arrays.asList(dictCode));
        return new Result(2000,"成功",null);
    }
}
